<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
        $time=time();
		$sql="select * from `".$oto."_youhui` where end_time>'{$time}' and `begin_time`<='{$time}' and is_effect='1'";
		//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$youhui[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($youhui))
		{
			for($i=0;$i<count($youhui);$i++)
			{
				$youhui_id=$youhui[$i]['id'];
				$sql="select * from `".$oto."_youhui_user_link` where youhui_id='{$youhui_id}' and u_is_effect='1' and user_id='{$user_id}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$youhui_user_link[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				$sql="select * from `".$oto."_youhui_use_record` where youhui_id='{$youhui_id}' and userId='{$user_id}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$youhui_use_record[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				$reco=0;
				if(isset($youhui_use_record[$i]))
				{
					for($j=0;$j<count($youhui_use_record[$i]);$j++)
					{
						if(date('Y-m-d',$youhui_use_record[$i][$j]['useTime'])==date('Y-m-d',$time))
						{
							$reco++;
						}
					}
				}
				if(!isset($youhui_user_link[$i]))
				{
					$youhui[$i]['youhui_user_link']=$reco;
				}
				else
				{
					$youhui[$i]['youhui_user_link']=$youhui_user_link[$i][0]['todayget'];
				}
			}
			for($i=0;$i<count($youhui);$i++)
			{
				$shopId=$youhui[$i]['supplier_id'];
				$sql="select shopName,shopId from `".$oto."_shops` where shopId='{$shopId}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$shops[$i][]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($shops[$i]))
				{
					$youhui[$i]['shopName']=$shops[$i][0]['shopName'];
					$youhui[$i]['shopId']=$shops[$i][0]['shopId'];
				}
				else
				{
					$youhui[$i]['shopName']="";
					$youhui[$i]['shopId']="";
				}
				if($youhui[$i]['youhui_scope']==1)
				{
					$youhui[$i]['msg']="全部商品";
				}
				if($youhui[$i]['youhui_scope']==2)
				{
					$shop_cat_id=$youhui[$i]['shop_cat_id'];
					$shop_cat_name="";
					$sql="select catName from `".$oto."_shops_cats` where catId='{$shop_cat_id}'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$shop_cat[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($shop_cat[$i]))
					{
						$shop_cat_name=$shop_cat[$i][0]['catName'];
					}
					$youhui[$i]['msg']="店铺分类：".$shop_cat_name;
				}
				if($youhui[$i]['youhui_scope']==3)
				{
					$goods_id=$youhui[$i]['good_id'];
					$sql="select goodsName from `".$oto."_goods` where goodsId='{$goods_id}'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$goods[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($goods[$i]))
					{
						$goodsName=$goods[$i][0]['goodsName'];
					}
					else $goodsName="";
					$youhui[$i]['msg']="商品：".$goodsName;
				}
				if($youhui[$i]['youhui_scope']==4)
				{
					$brand_id=$youhui[$i]['brand_id'];
					$brand_name="";
					$sql="select brandName from `".$oto."_brands` where brandId='{$brand_id}'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$brand[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($brand[$i]))
					{
						$brand_name=$brand[$i][0]['brandName'];
					}
					$youhui[$i]['msg']="品牌：".$brand_name;
				}
				if($youhui[$i]['youhui_scope']==5)
				{
					$cat_name="";
					$cat_id=$youhui[$i]['deal_cate_id'];
					$sql="select catName from `".$oto."_goods_cats` where catId='{$cat_id}'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$cat[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($cat[$i]))
					{
						$cat_name=$cat[$i][0]['catName'];
					}
					$youhui[$i]['msg']="商城分类：".$cat_name;
				}
			}
			$num=0;
			$num2=0;
			for($i=0;$i<count($youhui);$i++)
			{
				$s=$youhui[$i]['user_limit']-$youhui[$i]['youhui_user_link'];
	            $sy=$youhui[$i]['total_num']-$youhui[$i]['user_count'];
	            if($sy>=$s) $s=$s;
	            else $s=0;
	            $num+=$s;
	            for($j=0;$j<$s;$j++)
	            {
	            	$data[$num2]=$youhui[$i];
	            	$num2++;
	            }
			}
			if($num2==0) echo '[]';
			else echo json_encode($data);
		}
		else echo "[]";
		
?>